package com.tyk.bookstore.front.system.service.impl;


import com.tyk.bookstore.front.common.core.util.TreeUtil;
import com.tyk.bookstore.front.system.model.entity.AreaDo;
import com.tyk.bookstore.front.system.model.vo.AreaVo;
import com.tyk.bookstore.front.system.repository.AreaRepository;
import com.tyk.bookstore.front.system.service.AreaService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tyk
 * @description 行政区域管理
 */
@Service
@RequiredArgsConstructor
public class AreaServiceImp implements AreaService {

    private final AreaRepository areaRepository;

    @Override
    public List<AreaVo> getAreaTree() {
        // 查询所有行政区域
        List<AreaDo> areaList = areaRepository.selectList(null);
        // 构建树型结构并返回
        return TreeUtil.buildTree(
                areaList,
                AreaDo::getCode,
                AreaDo::getParentCode,
                it -> new AreaVo(it.getCode(), it.getName()),
                AreaVo::addChild
        );
    }
}
